<template>
  <div class="pagination">
    <button>上一页</button>
    <button v-show="startAndEnd.start > 1">1</button>
    <!-- 当连贯页码的一半向下取整  小于当前页码 -->
    <button v-show="startAndEnd.start > 2">···</button>

    <!-- <button>{{ PageNo - Math.trunc(continues / 2) }}</button> -->
    <!-- 向下取整 -->

    <!-- 由于我们设置了连贯页码,无论如何点击,我们相当于都是在点击连贯页码中的按钮,所以我们只需要将连贯页码的按钮设置是否高亮即可 -->
    <button @click="sendPageTotal(startAndEnd.start + index)" v-for="(item, index) in continues" :key="item" :class="{ active: pageNo === startAndEnd.start + index }">
      {{ startAndEnd.start + index }}
    </button>

    <button v-show="totalPage - startAndEnd.end > 1">···</button>

    <button v-show="totalPage > startAndEnd.end">{{ totalPage }}</button>

    <button @click="nextPage">下一页</button>

    <button style="margin-left: 30px">共 {{ total }} 条</button>
  </div>
</template>

<script>
// 需要的数据有,total:总数据,
// totalPage:总页码,
// PageNo:当前页码,
// continues:连贯页码
// pageSize:每页数据

import { mapGetters } from "vuex"

export default {
  name: "Pagination",
  data() {
    return {}
  },
  props: {
    total: {
      type: Number,
      default: 50,
    },
    pageNo: {
      type: Number,
      default: 1,
    },
    continues: {
      type: Number,
      default: 5,
    },
    pageSize: {
      type: Number,
      default: 5,
    },
  },
  computed: {
    // 计算总页码数
    totalPage() {
      const { total, pageSize } = this
      return Math.ceil(total / pageSize)
    },

    // 连贯页码  --->开始和结束的页码
    // 计算开始和结束的页码
    startAndEnd() {
      const { pageNo, continues, totalPage } = this
      let start
      let end
      // 特殊情况处理
      //   如果连贯页码大于总页码数
      if (continues > totalPage) {
        start = 1

        end = totalPage
      } else {
        // 如果都不符合,那就是正常情况
        start = pageNo - Math.trunc(continues / 2)
        end = pageNo + parseInt(continues / 2)

        // 如果连贯页码的数字,比第一页小
        //   [1,2,3,4,5,6,7,8,9]--> 总页码
        //   因为连贯页码中,当前页码一般都是中间的数字,假设连贯页码为5,点击第二页时,连贯页码的第一页是0,此时就不合理
        if (start < 1) {
          start = 1
          end = continues
        }
        if (end > totalPage) {
          // 理由同上,当点击第八页时,连贯页码的结束是10,但是总页是9
          start = totalPage - continues + 1
          end = totalPage
        }
      }
      return { start, end }
    },

    // ...mapState,
    ...mapGetters,
  },
  methods: {
    nextPage() {
      //   this.PageNo + 1
      //   console.log(this)
    },
    // sendPageTotal(q) {
    //   console.log(q)
    // },
    sendPageTotal(pageNo) {
      this.$emit("sendPageTotal", pageNo)
    },
  },
}
</script>

<style lang="less" scoped>
.pagination {
  text-align: center;
  button {
    margin: 0 5px;
    background-color: #f4f4f5;
    color: #606266;
    outline: none;
    border-radius: 2px;
    padding: 0 4px;
    vertical-align: top;
    display: inline-block;
    font-size: 13px;
    min-width: 35.5px;
    height: 28px;
    line-height: 28px;
    cursor: pointer;
    box-sizing: border-box;
    text-align: center;
    border: 0;

    &[disabled] {
      color: #c0c4cc;
      cursor: not-allowed;
    }

    &.active {
      cursor: not-allowed;
      background-color: #c81623;
      color: #fff;
    }
  }
  span {
    display: inline-block;
    line-height: 28px;
    font-size: 14px;
    color: gray;
    vertical-align: middle;
  }
}
</style>
